package com.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.erp.entity.Goods;
import com.example.erp.entity.InoutBoundDtl;
import com.example.erp.entity.StoreInfo;
import com.example.erp.entity.dto.ExcelDto;
import com.example.erp.mapper.GoodsMapper;
import com.example.erp.mapper.StoreInfoMapper;
import com.example.erp.service.IInoutBoundDtlService;
import com.example.erp.service.IStoreInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author rws
 * @since 2025-01-08
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Autowired
    private IInoutBoundDtlService inoutBoundDtlService;
    @Autowired
    private StoreInfoMapper storeInfoMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String time = now.format(formatter);
    @Override
    public void insertStoreInfo(List<ExcelDto> excelDtoList) {
        for(ExcelDto excelDto:excelDtoList){
            QueryWrapper<Goods> GoodsWrapper = new QueryWrapper<>();
            GoodsWrapper.allEq(Map.of("goods_name",excelDto.getGoodsName(),"units",excelDto.getUnits()));
            Goods goods=goodsMapper.selectOne(GoodsWrapper);

            StoreInfo storeInfo=new StoreInfo();
            storeInfo.setGoodsCode(goods.getGoodsCode())
                    .setNum(excelDto.getNum())
                    .setWeight(excelDto.getWeight())
                    .setBoundId(excelDto.getBoundId())
                    .setDtlId(excelDto.getDtlId())
                    .setInTime(time)
                    .setCreateTime(time)
                    .setUpdateTime(time);
            storeInfoMapper.insert(storeInfo);

            //更新入库表
            InoutBoundDtl inoutBoundDtl=new InoutBoundDtl();
            inoutBoundDtl.setDtlId(excelDto.getDtlId())
                        .setInoutTime(time)
                        .setInfoId(storeInfo.getInfoId());
            inoutBoundDtlService.updateById(inoutBoundDtl);

        }
    }
}
